﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    [ContentProperty(Name = "DataTemplate")]
    public class OfTypeControl : Grid
    {
        public OfTypeControl()
        {
            Loaded += OfTypeControl_Loaded;
        }

        void OfTypeControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (DataContext != null && DataContext.GetType().FullName.Replace('+','.').ToUpper().Equals(OfType.ToUpper()))
            {
                var content = (FrameworkElement)DataTemplate.LoadContent();
                content.DataContext = DataContext;
                Children.Add(content);
            }
        }

        public string OfType
        {
            get { return (string)GetValue(OfTypeProperty); }
            set { SetValue(OfTypeProperty, value); }
        }
        public static readonly DependencyProperty OfTypeProperty =
            DependencyProperty.Register("OfType", typeof(string), typeof(OfTypeControl), new PropertyMetadata(null));

        public DataTemplate DataTemplate
        {
            get { return (DataTemplate)GetValue(DataTemplateProperty); }
            set { SetValue(DataTemplateProperty, value); }
        }
        public static readonly DependencyProperty DataTemplateProperty =
            DependencyProperty.Register("DataTemplate", typeof(DataTemplate), typeof(OfTypeControl), new PropertyMetadata(null));
    }
}
